package com.atan.weblog.common.domain.mapper;

import com.atan.weblog.common.domain.dos.TagDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

public interface TagMapper extends BaseMapper<TagDO> {

    /**
     * 分页获取标签列表
     */
    default Page<TagDO> selectTagPageList(long current, long size, String name, LocalDate startDate, LocalDate endDate) {
        //封装分页对象
        Page<TagDO> page = new Page<>(current, size);
        //封装查询条件
        LambdaQueryWrapper<TagDO> wrapper = new LambdaQueryWrapper<>();

        //模糊查询
        wrapper.like(Objects.nonNull(name), TagDO::getName, name)
                .ge(Objects.nonNull(startDate), TagDO::getCreateTime, startDate)
                .le(Objects.nonNull(endDate), TagDO::getCreateTime, endDate)
                .orderByDesc(TagDO::getCreateTime);


        return selectPage(page, wrapper);
    }

    default List<TagDO> searchTag(String key){
        LambdaQueryWrapper<TagDO> wrapper = new LambdaQueryWrapper<TagDO>().like(TagDO::getName, key)
                .orderByDesc(TagDO::getCreateTime);
        List<TagDO> tagDOS = selectList(wrapper);
        return tagDOS;
    }

    /**
     * 根据标签id批量查询
     * @param tagIds 标签id集合
     * @return
     */
    default List<TagDO> selectByIds(List<Long> tagIds) {
        return selectList(Wrappers.<TagDO>lambdaQuery()
                .in(TagDO::getId, tagIds));
    }
}
